Imports System.Windows.Forms

Public Class dlgQuarter

    Dim QuarterID As String
    Private Sub btnUpdate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnUpdate.Click
        Dim sql As String



        If cbQuarterName.Text.Length < 2 Then
            MsgBox("The text is so short!!")
            cbQuarterName.Focus()
            Exit Sub
        End If
        'If db.getvalue("select * from Quarters where QuarterID = '" & cbQuarterName.Text.Replace("'", "") & "'").Tables(0).Rows.Count = 0 Then
        '    MsgBox("The Month " & cbQuarterName.Text & " is not found!!!")
        '    Exit Sub
        'End If
        If Not checkQuarterDate(dtpStartDate.Value, dtpEndDate.Value, QuarterID) Then
            MsgBox("The date is not available.")
            Exit Sub
        End If
        Try
            sql = "update Quarters set " _
                   & " qStartDate='" & dtpStartDate.Value _
                   & "',qEndDate='" & dtpEndDate.Value _
                   & "',qLimit=" & nupTotalofMeals.Value _
                   & ",QuarterID = '" & cbQuarterName.Text.Replace("'", "''") _
                   & "' where QuarterID = '" & QuarterID & "'"
            db.update(sql)
            MsgBox("the Month " & cbQuarterName.Text & " has been updated.")
            cbQuarterName.DataSource = db.getvalue("select * from Quarters").Tables(0)
            cbQuarterName.DisplayMember = "QuarterID"
        Catch ex As Exception
            MsgBox(ex.Message, MsgBoxStyle.Critical)
        End Try

    End Sub

    Private Sub btnNew_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnNew.Click
        Dim sql As String
        If cbQuarterName.Text.Length < 2 Then
            MsgBox("The text is so short!!")
            cbQuarterName.Focus()
            Exit Sub
        End If
        If db.getvalue("select * from Quarters where QuarterID = '" & cbQuarterName.Text.Replace("'", "") & "'").Tables(0).Rows.Count > 0 Then
            MsgBox("Duplicate!!!")
            Exit Sub
        End If
        If Not checkQuarterDate(dtpStartDate.Value, dtpEndDate.Value, cbQuarterName.Text) Then
            MsgBox("The date is not available.")
            Exit Sub
        End If
        Try
            sql = "insert into Quarters(QuarterID,qStartDate,qEndDate,qLimit)" _
                    & " values('" & cbQuarterName.Text.Replace("'", "''") _
                    & "','" & dtpStartDate.Value _
                    & "','" & dtpEndDate.Value _
                    & "'," & nupTotalofMeals.Value & ")"
            db.update(sql)

            MsgBox("the Month " & cbQuarterName.Text & " has been created.")
            cbQuarterName.DataSource = db.getvalue("select * from Quarters").Tables(0)
            cbQuarterName.DisplayMember = "QuarterID"
        Catch ex As Exception
            MsgBox(ex.Message, MsgBoxStyle.Critical)
        End Try

    End Sub


    Private Sub cbQuarterName_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cbQuarterName.SelectedIndexChanged
        Dim ds As DataSet

        ds = db.getvalue("select * from Quarters where QuarterID = '" & cbQuarterName.Text.Replace("'", "") & "'")
        If ds.Tables(0).Rows.Count > 0 Then
            dtpStartDate.Value = ds.Tables(0).Rows(0).Item("qstartdate")
            dtpEndDate.Value = ds.Tables(0).Rows(0).Item("qenddate")
            nupTotalofMeals.Text = ds.Tables(0).Rows(0).Item("qlimit")
            QuarterID = ds.Tables(0).Rows(0).Item("QuarterID").ToString
        End If
    End Sub

    Private Sub dlgQuarter_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        cbQuarterName.DataSource = db.getvalue("select * from Quarters").Tables(0)
        cbQuarterName.DisplayMember = "QuarterID"
    End Sub
    

    Private Sub dtpStartDate_ValueChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles dtpStartDate.ValueChanged
        dtpStartDate.Value = New Date(dtpStartDate.Value.Year, dtpStartDate.Value.Month, dtpStartDate.Value.Day, 0, 0, 0)
    End Sub

    Private Sub dtpEndDate_ValueChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles dtpEndDate.ValueChanged
        dtpEndDate.Value = New Date(dtpEndDate.Value.Year, dtpEndDate.Value.Month, dtpEndDate.Value.Day, 23, 59, 59)
    End Sub
End Class
